(function(window,document){
    function LS(){
    }

    var fn=LS.prototype;

    fn.setLocalStorage = function (name, val, namespace) {
        var setName = namespace ? namespace + '-' + name : name;
        if (localStorage) {
            localStorage.setItem(setName, val);
        }
        else {
            var obj = this.cookie2obj();
            obj[setName] = val;
            document.cookie = this.obj2cookie(obj);
        }
    };

    fn.getLocalStorage = function (name, namespace) {
        var val;
        var getName = namespace ? namespace + '-' + name : name;
        if (localStorage) {
            val = localStorage.getItem(getName);
        }
        else {
            val = this.cookie2obj()[getName];
        }
        return val;
    };

    fn.removeLocalStorage = function (name, namespace) {
        var truename = namespace ? namespace + '-' + name : name;
        if (localStorage) {
            localStorage.removeItem(truename);
        }
        else {
            var cookieObj = this.cookie2obj();
            if (cookieObj[truename]) {
                delete cookieObj[truename];
                document.cookie = this.obj2cookie(cookieObj);
            }
        }
    };

    fn.cookie2obj = function () {
        var cookieArr = document.cookie.split(';');
        var resultobj = {};
        for (var i = 0, len = cookieArr.length; i < len; i++) {
            var cookieItem = cookieArr[i].split('=');
            resultobj[cookieItem[0]] = cookieItem[1];
        }
        return resultobj;
    };

    fn.obj2cookie = function (obj) {
        var result = '';
        for (var i in obj) {
            result = result.concat(i, '=', obj[i], '; ');
        }
        if (result.length > 0) {
            result = result.slice(0, result.length - 2);
        }
        return result;
    };

    if ( typeof define === "function" && define.amd ) {
        define( [],  function () { return new LS(); } );
    }else if(typeof module === "object" && module && typeof module.exports === "object"){
        module.exports = new LS();
    }else{
        window.LS=new LS();
    }
})(window,document);